package com.example.shiXun.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.shiXun.base.result.Results;
import com.example.shiXun.domain.SysPermission;
import com.example.shiXun.dto.RoleDto;
import com.example.shiXun.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * @author ：meet
 * @date ：Created in 2020/4/13 15:17
 */
@Controller
@RequestMapping("permission")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/listAllPermission")
    @ResponseBody
    public Results<JSONArray> listAllPermission() {
        return permissionService.listAllPermission();
    }

    @GetMapping("/listAllPermissionByRoleId")
    @ResponseBody
    public Results<SysPermission> listAllPermissionByRoleId(RoleDto roleDto) {
        log.info(getClass().getName() + " : param =  " + roleDto);
        return permissionService.listByRoleId(roleDto.getId().intValue());
    }

    @GetMapping("/menuAll")
    @ResponseBody
    public Results<SysPermission> getMenuAll() {
        return permissionService.getMenuAll();
    }

    @GetMapping("/add")
    public String addPermission(Model model){
        model.addAttribute("sysPermission",new SysPermission());
        return "permission/permission-add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Results<SysPermission> savePermission(@RequestBody SysPermission permission){
        return permissionService.save(permission);
    }

    @GetMapping("/edit")
    public String editPermission (Model model, SysPermission permission){
        model.addAttribute("sysPermission", permissionService.getPermissionById(permission.getId()));
        return "permission/permission-add";
    }

    @PostMapping("/edit")
    @ResponseBody
    public Results<SysPermission> updatePermission(@RequestBody SysPermission permission){
        return permissionService.updatePermission(permission);
    }
    @PostMapping("/delete")
    @ResponseBody
    public Results deletePermission(SysPermission permission){
        return permissionService.delete(permission.getId());
    }


    @GetMapping("/menu")
    @ResponseBody
    public Results<SysPermission> getMenu(Long userId){
        return permissionService.getMenu(userId);
    }
}
